package com.dut.bbs.entity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.dut.bbs.util.URLConstant;
import com.dut.bbs.web.HtmlSource;
import com.dut.bbs.web.HttpClientFactory;

import android.util.Log;


public class MailEntity {
	/**
	 * 
	 * @param id 收信人ID
	 * @param title 信件标题
	 * @param content 内容
	 * @param backup 是否备份到发信箱
	 * @return
	 */
	public static boolean mail(String id,String title,String content,boolean backup){
		String httpUrl = "http://bbs.dlut.edu.cn/nforum/m/mail/send";
		HttpPost httppost = new HttpPost(httpUrl);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("content",content));
		params.add(new BasicNameValuePair("title",title));
		params.add(new BasicNameValuePair("id",id));
		if(backup)
			params.add(new BasicNameValuePair("backup","on"));
		try{
			HttpEntity httpentity = new UrlEncodedFormEntity(params,"utf-8");
			httppost.setEntity(httpentity);
			HttpClient httpclient = HttpClientFactory.getHttpClient();
			HttpResponse httpResponse = httpclient.execute(httppost);
			Log.e("Mail", httpResponse.toString());
			if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				String strResult = EntityUtils.toString(httpResponse.getEntity());
				Log.e("Mail", strResult);
				httppost.abort();
				if(strResult.contains("<div class=\"sp hl f\">邮件发送成功</div>"))
					return true;
				else
					return false;
			}else
				return false;
		}catch(Exception e){
			Log.e("Mail", e.getMessage().toString());
			return false;
		}
	}
	public static boolean delmail(String url){
		try {
			String html = HtmlSource.getHtml_data(URLConstant.URL_BASE+url);
			if(html.contains("<div class=\"sp hl f\">邮件删除成功</div>"))
				return true;
			else
				return false;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		}
		
	}
}

